package com.hp.dao;

import com.hp.entity.Patient;
import com.hp.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class PatientDao {
    /**
     * 功能1：用于检查身份证号是否重复
     * 功能2：用于挂号时查询病人信息
     * @param idCard
     * @return
     * @throws RuntimeException
     */
    public Patient getPatientByIdCard(String idCard) throws RuntimeException{
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT id,name,birthday,sex,address,phone,id_card as idCard,is_medicare as isMedicare," +
                "id_medicare as idMedicare,created_time as createdTime,updated_time as updatedTime " +
                "FROM patient WHERE id_card=?";
        try {
            return queryRunner.query(sql,new BeanHandler<Patient>(Patient.class),idCard);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 功能1：查询医保卡是否存在
     * @param idMedicare
     * @return
     * @throws RuntimeException
     */
    public Integer getIsMedicareByIdMedicare(String idMedicare) throws RuntimeException{
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT count(*) FROM patient WHERE id_Medicare=?";
        try {
            return Integer.parseInt(queryRunner.query(sql,new ScalarHandler(),idMedicare).toString());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 功能1：添加病人信息
     * @param patient
     * @return
     */
    public boolean addPatient(Patient patient) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="INSERT INTO patient(id,name ,birthday,sex,address,phone,id_card,is_medicare," +
                "id_medicare,created_time,updated_time) VALUES(NULL,?,?,?,?,?,?,?,?,?,?)";
        try {
           return queryRunner.update(sql,
                   patient.getName(),
                   patient.getBirthday(),
                   patient.getSex(),
                   patient.getAddress(),
                   patient.getPhone(),
                   patient.getIdCard(),
                   patient.getIsMedicare(),
                   patient.getIdMedicare(),
                   patient.getCreatedTime(),
                   patient.getUpdatedTime()
           )>0?true:false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Patient getPatientById(Integer patientId) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT id,name,birthday,sex,address,phone,id_card as idCard,is_medicare as isMedicare," +
                "id_medicare as idMedicare,created_time as createdTime,updated_time as updatedTime " +
                "FROM patient WHERE id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<Patient>(Patient.class),patientId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
